<template>
  <div class="departments-container">
    <el-card>
      <!-- 标题 -->
      <el-row
        type="flex"
        justify="space-between"
        align="middle"
        style="height: 40px; width: 100%"
      >
        <el-col :span="20">传智教育</el-col>
        <el-col :span="4">
          <el-row type="flex" justify="end">
            <el-col>负责人</el-col>
            <!-- <el-col :span="12">操作</el-col> -->
            <!-- 下拉菜单 element -->
            <el-col>
              <el-dropdown>
                <span> 操作<i class="el-icon-arrow-down" /> </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item
                    @click.native="addDep('')"
                  >添加子部门</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </el-col>
      </el-row>

      <!-- 树形结构 -->
      <el-tree :data="data" :props="defaultProps" :indent="0">
        <template slot-scope="{ data }">
          <el-row
            type="flex"
            justify="space-between"
            align="middle"
            style="height: 40px; width: 100%"
          >
            <el-col :span="20">{{ data.name }}</el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <el-col>{{ data.manager }}</el-col>

                <el-col>
                  <!-- 下拉菜单 element -->
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <!-- 下拉菜单 -->
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item
                        @click.native="addDep(data.id)"
                      >添加子部门</el-dropdown-item>
                      <el-dropdown-item
                        @click.native="editDep(data)"
                      >编辑部门</el-dropdown-item>
                      <el-dropdown-item
                        @click.native="delDep(data.id)"
                      >删除部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </el-card>

    <!-- 添加部门 -->
    <el-dialog
      :title="isEdit ? '编辑部门' : '添加部门'"
      :visible.sync="dialogFormVisible"
    >
      <addDepDia
        v-if="dialogFormVisible"
        ref="addDepDia1"
        :dialog-form-visible.sync="dialogFormVisible"
        :pid="pid"
        :is-edit="isEdit"
        :row="row"
        @success="success"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
// import { transArray} from '@/utils'
import { transArray2 } from '@/utils'
import addDepDia from './addDepDia.vue'
export default {
  components: {
    addDepDia
  },
  data() {
    return {
      data: [],
      defaultProps: { children: 'children', label: 'name' },
      dialogFormVisible: false,
      pid: '',
      isEdit: false,
      row: {}
    }
  },
  created() {
    this.loadDep()
  },
  methods: {
    async loadDep() {
      const res = await getDepartments()
      console.log(res, 88888888)
      if (res.code === 10000) {
        // 将数据转换成树形结构
        // this.data = transArray2(res.data.depts)
        this.data = transArray2(res.data.depts)
        console.log(this.data, 7777)
      }
    },
    // 添加部门
    addDep(pid) {
      /* 步骤分析
      1.点击按钮
      2.显示对话框
      3.对话框中表单结构布局
      4.表单数据绑定+获取负责人数据
        4-1 封装api接口函数
        4-2 展示负责人数据
      5.表单校验
        5-1 表单校验
        5-2 兜底校验  ===点击确认按钮
          5-2-1 封装api接口函数
          5-2-2 发送请求
          5-2-3 请求成功后，关闭对话框，清空表单数据，刷新页面

      */

      //  显示对话框
      this.dialogFormVisible = true
      // console.log(this.$refs,778899);
      // 获取pid
      this.pid = pid
      this.isEdit = false // 点击的是添加按钮
    },
    success() {
      // 添加部门成功后执行
      this.dialogFormVisible = false
      // this.$refs.addDepDia1 获取子组件中的this
      this.$refs.addDepDia1.reset()
      this.loadDep()
      // console.log(this.$refs.addDepDia1.son,44444);
    },
    // 删除部门
    async delDep(id) {
      /*
      步骤分析:
       1.点击按钮
       2.显示对话框
       3.点击取消，关闭对话框
       4.点击确认按钮
         4.1 获取当前点击行的id
         4.2 封装api接口函数
         4.3 调用api接口函数
         4.4 成功后,刷新页面
      */

      //  显示对话框  MessageBox 弹框
      const res = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }
      ).catch(() => {
        this.$message({ type: 'info', message: '已取消删除' })
      })
      if (res === 'confirm') {
        // 表示点击的是确认按钮
        // 4.3 调用api接口函数
        const result = await delDepartment(id)
        // console.log(result,889999);
        if (result.code === 10000) {
          // 4.4 成功后,刷新页面
          this.loadDep()
        }
      }
    },
    // 编辑部门
    editDep(row) {
      // console.log(row, 223344)
      /*
      步骤分析
      1.点击编辑按钮
      2.显示对话框
        2.1 判断是点击的添加还是点击的编辑  定义一个值:isEdit:false
        2.2 如果是: 添加 isEdit=false
          2.2.1 对话框表单不显示内容
          2.2.2 标题显示: 添加
          2.2.3 点击对话框确认按钮:发送添加的请求
        2.3 如果是: 编辑 isEdit=true
          2.3.1 对话框表单显示内容
          2.3.2 标题显示: 编辑
          2.3.3 点击对话框确认按钮: 发送编辑的请求

      */

      this.isEdit = true // 点击的是编辑按钮
      //  2.显示对话框
      this.dialogFormVisible = true
      // 获取当前行的数据
      this.row = row
    }
  }
}
</script>
<style lang="scss" scoped>
.departments-container {
  padding: 20px;
  .el-card {
    padding: 30px 120px;
  }
}
</style>
